﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace PoemonAdventures
{
    public class Player
    {
        enum PlayerState
        {
            Standing,
            Up,
            Down,
            Left,
            Right
        }

        PlayerState playerState;

        Texture2D texture;
        Vector2 positionOnMap;
        public Poemon myPoemon { get; set; }

        Point currentframe = new Point(0, 0);
        Point framesize = new Point(32, 48);
        Point sheetsize = new Point(4, 4);
        int animationDelay = 0;
        int speedOnMap = 2;

        public Player(Texture2D playergfx, Texture2D poemonGfx, Vector2 position)
        {
            this.texture = playergfx;
            this.positionOnMap = position;
            myPoemon = new Poemon(poemonGfx);
            playerState = PlayerState.Standing;
        }

        public void Update()
        {
            KeyboardState ks = Keyboard.GetState();
            if (animationDelay == 10)
            {
                ++currentframe.X;
                animationDelay = 1;
            }
            if (currentframe.X >= sheetsize.X)
            {
                currentframe.X = 0;
            }
            if (ks.IsKeyDown(Keys.W))
            {
                playerState = PlayerState.Up;
            }
            else if (ks.IsKeyDown(Keys.A))
            {
                playerState = PlayerState.Left;
            }
            else if (ks.IsKeyDown(Keys.S))
            {
                playerState = PlayerState.Down;
            }

            else if (ks.IsKeyDown(Keys.D))
            {
                playerState = PlayerState.Right;
            }
            else
            {
                playerState = PlayerState.Standing;
            }
            switch (playerState)
            {
                case (PlayerState.Right):
                    currentframe.Y = 2;
                    positionOnMap.X += speedOnMap;
                    ++animationDelay;
                    break;
                case (PlayerState.Left):
                    currentframe.Y = 1;
                    positionOnMap.X -= speedOnMap;
                    ++animationDelay;
                    break;
                case (PlayerState.Up):
                    currentframe.Y = 3;
                    positionOnMap.Y -= speedOnMap;
                    ++animationDelay;
                    break;
                case (PlayerState.Down):
                    currentframe.Y = 0;
                    positionOnMap.Y += speedOnMap;
                    ++animationDelay;
                    break;
                case (PlayerState.Standing):
                    positionOnMap.X += 0;
                    positionOnMap.Y += 0;
                    ++animationDelay;
                    currentframe.X = 0;
                    break;
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, positionOnMap, new Rectangle((currentframe.X * framesize.X), (currentframe.Y * framesize.Y), framesize.X, framesize.Y), Color.White);
        }
    }
}
